<template>
  <!-- 授权用户 -->
  <el-dialog v-dialogDrag title="选择用户" :visible.sync="visible" width="800px" top="5vh" append-to-body :close-on-click-modal='false' :close-on-press-escape='false'>
    <div class="content-wrap my-search">
      <el-form :model="queryParams" size="mini" ref="queryForm" :inline="true">
        <el-form-item label="用户名" prop="loginId">
          <el-input v-model="queryParams.loginId" placeholder="请输入用户名" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入用户姓名" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="userList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户名" align="left" prop="loginId" :show-overflow-tooltip="true" />
        <el-table-column label="用户姓名" align="left" key="name" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="邮箱" align="left" key="email" prop="email" width="120">

          <template slot-scope="scope">
            <span>{{scope.row.email||'-'}}</span>
          </template>
        </el-table-column>

        <el-table-column label="所属网站" align="center" key="siteId" prop="siteId" width="120" :formatter="siteIdFormat" show-overflow-tooltip v-if="false" />

      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { unallocatedUserList, authUserSelectAll } from "@/api/system/role";
export default {
  props: {
    // 角色编号
    roleId: {
      type: [Number, String],
    },
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      userIds: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      userList: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        loginId: undefined,
      },
    };
  },
  created() {
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.roleId = this.roleId;
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map((item) => item.userId);
    },
    // 查询表数据
    getList() {
      unallocatedUserList(this.queryParams).then((res) => {
        this.userList = res.data.list;
        this.total = res.data.totalCount;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权用户操作 */
    handleSelectUser() {
      const roleId = this.queryParams.roleId;
      const userIds = this.userIds.join(",");
      authUserSelectAll({ roleId: roleId, userIds: userIds }).then((res) => {
        this.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      });
    },
  },
};
</script>
